package it.maggioni.sandbox.hibernate4;

import it.maggioni.sandbox.hibernate4.dto.UserDetails01;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class Ex01 {
	
	/**
	 * Come primo esempio creo una tabella per imparare ad utilizzare la 
	 * session factory
	 * @param args
	 */
	
	public static void main(String[] args) {
		UserDetails01 userDetails = new UserDetails01();
		userDetails.setUserId(1);
		userDetails.setUserName("First User");

		Configuration configuration = new Configuration().configure("/"+Ex01.class.getPackage().getName().replace(".","/")+"/hibernate-"+Ex01.class.getSimpleName()+".cfg.xml");
	    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
	    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	    
	    Session session = sessionFactory.openSession();
	    session.beginTransaction();
	    session.save(userDetails);
	    session.getTransaction().commit();
	    session.close();
	    
	    sessionFactory.close();
	}

}
